#pragma once
#include "FileStream.h"
#include "Log.h"
#include "ADTCreator.h"

namespace ADTHandle
{
	class _declspec(dllexport) ADT
	{
	private:
		IO::IO strm;
		Stasi::Log logger;
		Creator::ADTCreator adt_C;

		MVER ver;
		MHDR hdr;
		MH2O h2o;
		std::vector<MH2O::MH2O_H> h2o_h;
		std::vector<MH2O::MH2O_I> h2o_i;
		//std::vector<MH2O::MH2O_D> h2o_d;
		std::vector<MH2O::MH2O_R> h2o_r;
		std::vector<MCNK> cnk;
		std::vector<MCVT> cvt;
		std::vector<MCSE> cse;
		MFBO fbo;
	public:	   
		ADT(){ 	};

		ADT(int flag, const std::string& Filename)
		{
			switch(flag)
			{
			case 0:
				if(!strm.B_StreamIN(Filename))
				{
					logger << "Couldn't open File, stops here. \n";
					throw std::exception("Cannot Open File");
				}
				logger << "Created Log-File, begin to report stuff. \n";
				break;
			default:
				if(!strm.B_StreamOUT(Filename))
				{
					logger << "Cannot create Stream for File. \n";
					throw std::exception("Cannot Create File");
				}
				logger << "Created Log-File, begin to report stuff. \n";
				break;
			}
		}

		~ADT(){ strm.Close(); };

		template <typename T>
		T SearchChunkAndGetInfo(const std::string& ChunkName) //Reads the File Completly and if there more then 1 Chunks of the Same name,
		{										//it will only Show The first avaible Chunk-adress
			uint32 defaultStart = 0;		   //Not avaible for Structs with vectors or arrays in it!!!
			uint32 defaultEnd = strm.GetSize();
			logger << "File Size: ";
			logger >> defaultEnd;
			logger >> "\n";
			uint32 ptr = strm.SearchChunk(ChunkName);
			logger << "Chunk-Search: ";
			logger >> ChunkName >> "at" >> ptr >> "\n";
			strm.SetOffset(ptr);
			logger << "Reading Chunk: ";
			logger >> ChunkName >> "\n";
			T ret = strm.Read<T>(ret);
			return ret;
		}

		template <typename T>
		T SetAndRead(uint32 ptr, T& ret)
		{
			ret = strm.ReadAt(ret, ptr);
			return ret;
		}

		template <typename T>
		void WriteChunk(T& chunkinfo)
		{
			strm.Write<T>(chunkinfo);
		}

		bool CreateADT(); //still thinking about it, cause the new expansion will bring changes to the wowfiles
	};
}